﻿CREATE PROCEDURE [dbo].[select_buyers_sellers]
	@year int,
	@month_from int,
	@month_to int
AS
	declare @result_table table(result_id bigint identity(1,1) primary key,
								result_seller nvarchar(20) not null,
								result_buyer nvarchar(20) not null,
								result_weight decimal(5,1) default(0) not null);

	declare @seller nvarchar(20);
	declare @buyer nvarchar(20);
	declare @weight decimal(5,1);

	declare inout_cursor cursor for
	select inout_customer, inout_ncust, inout_out from tb_inout
	where inout_out != 0 and inout_ncust != '';
	open inout_cursor;
	fetch next from inout_cursor into @seller, @buyer, @weight;
	while @@fetch_status = 0 begin
	
		insert into @result_table(result_seller, result_buyer, result_weight)
		values(@seller,@buyer,@weight);
	
		fetch next from inout_cursor into @seller, @buyer, @weight;
	end
	close inout_cursor;
	deallocate inout_cursor;
	
	select * from @result_table;